<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
	function createComprisonFunction(propertyName){
		
		return function(object1, object2){
			
			var value1=object1[propertyName];
			
			var value2=object2[propertyName];
			
			if(value1<value2){
				return -1;	
			}else if(value1>value2){
				return 1;
			}else{
				return 0;	
			}	
		};
	};
	
	var arr1=[{name:'yousi', age:18},{name:'blue', age:88}];
	
	arr1.sort(createComprisonFunction('name'));
	
	console.log(arr1);
	
	var arr2=[5,3,7,1,9];
	/*function compare(value1, value2){
		
		if(value1<value2){
			
			return -1;	
			
		}else if(value1>value2){
			
			return 1;	
			
		}else{
			
			return 0;	
			
		}
		return value1-value2;
	};*/
	
	//var result=compare(5, 10);
	//var result=arr2.sort(function compare(value1,value2){return value1-value2});
	//console.log(result);
	//创建函数
	var compareNames=createComprisonFunction('name');
	
	var result=compareNames({name:'Nicholas'}, {name:'Greg'});
	
	//解除对匿名函数的引用(以便释放内存)
	compareNames=null;
	
	console.log(compareNames);
	console.log(result);
	
	//闭包会携带包含它的函数的作用域，因此会比其他函数占用更多的内存。过多使用闭包会导致内存使用过多，只有绝对必要时再使用闭包，V8优化后的js引擎会尝试回收被闭包占用的内存，但是还是要慎重使用闭包
	
</script>
</head>

<body>
</body>
</html>
 